<?php
namespace Interf\Controller;

use Think\Controller;


class  TecinfoController extends Controller
{
    public function _initialize()
    {
        header("Access-Control-Allow-Origin:*");
    }

//    根据姓名性别等关键字，查出它所有的字段；
    public function  index(){
        $users = I('name');
        $pwd = I('pass');

        $db = M("cartecher");
        $list = $db->select();
        echo json_encode(array("status" => "ok", "count" => '',"teclist" => $list, 'info' => '成功','user'=>$users,'pwd'=>$pwd), JSON_UNESCAPED_UNICODE);
    }
    public function index3()
    {
        if (IS_POST) {
            $num = I('num', 20);
            $k = I('key');
            $page = I('page', 0);

            $list = array();
            if ($k != null) {
                $db = M("cartecher");
                $where['_string'] = "(xh like '%$k%')  OR (xm like '%$k%') OR (nj like '%$k%') OR (zy like '%$k%')  OR (school like '%$k%') OR (class like '%$k%') OR (idcard like '%$k%')";
                $count = $db->where($where)->count();
                $list = $db->field("xm,xh,xb,birth,school,class,jg")->where($where)->order('xh ASC')->limit($page * $num, $num)->select();

                $thisyear=Date('Y');
                for($i=0;$i<count($list);$i++){
//              给查出来的每条数据，对应的加一个照片
                    $sno=$list[$i]['xh'];
                    $year = substr( $sno, 0, 4);
                    $adress=C('PHOTO_PATH') . '/' . $year . '/' . $sno . '.jpg';
                    if (file_exists($_SERVER['DOCUMENT_ROOT']. '/'.'jw'.C('PHOTO_PATH') . '/' . $year . '/' . $sno . '.jpg')) {
                        $photo =__ROOT__ . $adress;
                    }else{
                        $photo = __ROOT__."/public/img/default.jpg";
                    }
                    $list[$i]['pic']='http://192.168.1.66'.$photo;

//                给查出来的每条数据算算年龄，加一个对应的年龄
                    $list[$i]['birth']=substr($list[$i]['birth'],0,10);
                    $birthyear=$list[$i]['birth'];
                    $age=$thisyear-$birthyear;
                    $list[$i]['age']=$age;
                }

                $allpages=floor($count/$num);
                if($count<=$num){   /*查出结果小于一屏的数量*/
                    echo json_encode(array("status" => "ok",'count'=>$count,"list" => $list,'info' => '成功','more'=>false,'moreinfo'=>'不满一屏'), JSON_UNESCAPED_UNICODE);
                }else{
                    if($page<$allpages){
                        echo json_encode(array("status" => "ok",'count'=>$count,"list" => $list,'info' => '成功','more'=>true,'moreinfo'=>'点击加载更多'), JSON_UNESCAPED_UNICODE);
                    }else{
                        echo json_encode(array("status" => "ok",'count'=>$count,"list" => $list,'info' => '成功','more'=>true,'moreinfo'=>'没有更多了'), JSON_UNESCAPED_UNICODE);
                    }
                }
            }else{
                echo json_encode(array("status" => "ok",'count'=>0,"list" => '','info' => '未输入关键字'), JSON_UNESCAPED_UNICODE);
            }
        }else{
            echo json_encode(array("status" => "ok", "count" => '',"list" => '', 'info' => '未用post方法提交'), JSON_UNESCAPED_UNICODE);
        }
    }
}